﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Members;
using VK.Comm;

namespace VK.Casemeet.Web.Admin.Members
{
    public partial class MemberEdit : System.Web.UI.Page
    {
        public int memberID = 0;
        public int area1, area2, area3;
        public string imgUrl = "Null.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="会员列表",Url="Index.aspx"},
                new MapPath{ Text="编辑会员",Url="#"}
            };

            memberID = Util.GetQueryInt("MemberID");

            if (!IsPostBack)
            {
                MemberHelper.BindSexRad(radSex);
                MemberHelper.BindProfessionDDL(ddlProfession, OptionType.Select);
                MemberHelper.BindEducationDDL(ddlEducation, OptionType.Select);
                MemberHelper.BindIncomeDDL(ddlIncome, OptionType.Select);
                MemberHelper.BindMarriageDDL(ddlMarriage, OptionType.Select);
                MemberHelper.BindStateDDL(ddlState, OptionType.Select);

                LoadData();
            }
        }

        internal void LoadData()
        {
            if (memberID != 0)
            {
                MemberEntity member = MemberBLL.GetMember(memberID);
                txtEmail.Text = member.Email;
                txtPassword.Text = DES.Decrypt(member.Password, CS.MemberPassKey);
                txtNickname.Text = member.Nickname;
                txtName.Text = member.Name;
                radSex.SelectedValue = member.Sex.ToString();
                txtBirthday.Text = Util.FormatDateTime(member.Birthday, DateTimeStyle.ShortDate2);
                txtQq.Text = member.QQ;
                txtPhone.Text = member.Phone;
                area1 = member.ProvinceID;
                area2 = member.CityID;
                area3 = member.AreaID;
                txtAddress.Text = member.Address;
                ddlProfession.SelectedValue = member.Profession.ToString();
                ddlEducation.SelectedValue = member.Education.ToString();
                ddlIncome.SelectedValue = member.Income.ToString();
                ddlMarriage.SelectedValue = member.Marriage.ToString();
                txtCreated.Text = Util.FormatDateTime(member.Created, DateTimeStyle.ShortDate2);
                ddlState.SelectedValue = member.State.ToString();
                imgUrl = member.ImgUrl;
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            MemberEntity member = LoadEntity();
            member.MemberID = memberID;

            if (MemberBLL.UpdateMember(member) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected MemberEntity LoadEntity()
        {
            MemberEntity member = new MemberEntity();
            member.Email = txtEmail.Text;
            member.Password = DES.Encrypt(txtPassword.Text, CS.MemberPassKey);
            member.Nickname = txtNickname.Text;
            member.Name = txtName.Text;
            member.Sex = int.Parse(radSex.SelectedValue);
            member.Birthday = Util.ConverDateTime(txtBirthday.Text);
            member.QQ = txtQq.Text;
            member.Phone = txtPhone.Text;
            member.ProvinceID = Util.GetFormInt("ddlArea1");
            member.CityID = Util.GetFormInt("ddlArea2");
            member.AreaID = Util.GetFormInt("ddlArea3");
            member.Address = txtAddress.Text;
            member.Profession = int.Parse(ddlProfession.SelectedValue);
            member.Education = int.Parse(ddlEducation.SelectedValue);
            member.Income = int.Parse(ddlIncome.SelectedValue);
            member.Marriage = int.Parse(ddlMarriage.SelectedValue);
            member.Created = Util.ConverDateTime(txtCreated.Text);
            member.State = int.Parse(ddlState.SelectedValue);
            member.ImgUrl = Util.GetFormString("hidImgUrl");

            return member;
        }
    }
}